package com.sa.web.behind.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionSupport;
import com.sa.web.bean.AdminBean;
import com.sa.web.dao.service.AdminInfoDao;
import com.sa.web.util.Page;
import com.sa.web.util.Util;

public class AdminManageAction extends ActionSupport {
	/** 序列 */
	private static final long serialVersionUID = 1L;
	/** 用户信息dao*/
	private AdminInfoDao adminInfoDao;
	/** 用户信息bean*/		
	private AdminBean adminBean;
	/** 设置管理员信息集合 */
	List<AdminBean> adminList;
	/** easyUi对象*/
	private JSONArray rows;
	/** 信息总数*/
	private JSONArray total;
	/** 获取返回值*/
	private JSONArray msg;
	/** 管理员姓名*/
	private String adminName;
	/** 管理员密码*/
	private String password;
	/** 学生ID*/
	private String adminId;
	
   /**
    * 初始化管理员查询页面
    * @return success
    * @throws Exception
    */
	public String initAdminFind() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 初始化管理员更新页面
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String initAdminInfoUpdate() throws Exception {
		setAdminBean((adminInfoDao.getAdminInfo(Util.getRequest().getParameter("adminId"))));
		return SUCCESS;
	}
	
	/**
	 * 初始化添加管理员页面
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String initAdminInfoAdd() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 添加管理员信息
	 * @return success
	 * @throws Exception
	 */
	public String addAdminInfo() throws Exception{
		adminBean.setAdminId(Util.getUUID());
		if(StringUtils.isEmpty(adminBean.getAdminName())){
			msg = Util.returnMsg("用户名不能为空，请重新输入");
		}
		else if(StringUtils.isEmpty(adminBean.getPassword())){
			msg = Util.returnMsg("密码不能为空，请重新输入");			
		}
		else if(adminBean.getAdminName().length()>20){
			msg = Util.returnMsg("用户名不能超过20位，请重新输入");
		}
		else if(adminBean.getPassword().length()>20){
			msg = Util.returnMsg("密码不能超过20位，请重新输入");
		}
		else{
		boolean result=	adminInfoDao.addAdminInfo(adminBean);
		msg = result?Util.returnMsg("添加成功"):Util.returnMsg("添加失败");
		}

		return SUCCESS;
	}
	
	/**
	 * 修改管理员信息
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String updateAdminInfo() throws Exception{
		adminInfoDao.updateAdminInfo(adminBean);
		return SUCCESS;
	}
	
	/**
	 * 查找管理员信息
	 * @return json
	 * @throws Exception
	 */
	public String findAdminInfoList()  throws Exception{
		adminList = adminInfoDao.getAdminInfoList( Util.getRequest().getParameter("adminName"),Page.getStarIndex(),Page.getPreIndex());
		 rows = JSONArray.fromObject(adminList);
		 total = JSONArray.fromObject(adminInfoDao.getAdminInfoCount(Util.getRequest().getParameter("userName")));
		 return Util.jSONSuccess();
	}
	
	
	/**
	 * 删除管理员信息
	 * @return json
	 * @throws Exception
	 */
	public String deleteAdminInfo() throws Exception {
		adminInfoDao.deleteAdminInfo(Util.getRequest().getParameter("adminId")==null?"":Util.getRequest().getParameter("adminId").toString());
		msg = JSONArray.fromObject("[\"删除成功\"]");
		return Util.jSONSuccess();
	}

/**************************getter and setter****************************************************************************************************/
	
	public AdminInfoDao getAdminInfoDao() {
		return adminInfoDao;
	}

	public void setAdminInfoDao(AdminInfoDao adminInfoDao) {
		this.adminInfoDao = adminInfoDao;
	}

	public AdminBean getAdminBean() {
		return adminBean;
	}

	public void setAdminBean(AdminBean adminBean) {
		this.adminBean = adminBean;
	}

	public List<AdminBean> getAdminList() {
		return adminList;
	}

	public void setAdminList(List<AdminBean> adminList) {
		this.adminList = adminList;
	}

	public JSONArray getTotal() {
		return total;
	}

	public void setTotal(JSONArray total) {
		this.total = total;
	}

	public String getAdminName() {
		return adminName;
	}

	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}

	public String getAdminId() {
		return adminId;
	}

	public void setAdminId(String adminId) {
		this.adminId = adminId;
	}

	public JSONArray getRows() {
		return rows;
	}

	public JSONArray getMsg() {
		return msg;
	}
	
	
}
